import { defHttp } from '/@/common/util/axios';
import { Modal } from 'ant-design-vue';
import { getTenantId } from '/@/common/util/auth';

enum Api {
  list = '/sys/tenant/getPage',
  save = '/sys/tenant/add',
  edit = '/sys/tenant/edit',
  get = '/sys/tenant/getId',
  delete = '/sys/tenant/delete',
  deleteBatch = '/sys/tenant/deleteBatch',
  getCurrentUserTenant = '/sys/tenant/getCurrentUserTenant',
  deleteTenantUser = '/sys/tenant/deleteTenantUser',
  getRecyclePage = '/sys/tenant/getRecyclePage',
  deleteLogicTenant = '/sys/tenant/deleteLogicTenant',
  revertLogicTenant = '/sys/tenant/revertLogicTenant',
  
  //新增、编辑用户租户
  saveUser = '/sys/user/add',
  editUser = '/sys/user/edit',
}

/**
 * 查询租户列表
 * @param params
 */
export const getTenantList = (params) => {
  return defHttp.get({ url: Api.list, params });
};

/**
 * 保存或者更新租户
 * @param params
 */
export const saveOrUpdateTenant = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 查询租户详情
 * @param params
 */
export const getTenantById = (params) => defHttp.get({ url: Api.get, params });

/**
 * 删除租户
 * @param params
 */
export const deleteTenant = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除租户
 * @param params
 */
export const batchDeleteTenant = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 获取登录用户部门信息
 */
export const getCurrentUserTenant = (params?) => defHttp.get({ url: Api.getCurrentUserTenant, params });

/**
 * 用户离开租户
 * @param params
 */
export const deleteTenantUser = (params, handleSuccess) => {
  Modal.confirm({
    title: '请离',
    content: '是否请离该用户',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.put({ url: Api.deleteTenantUser, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 获取租户回收站的列表
 * @param params
 */
export const getRecyclePage = (params) => {
  return defHttp.get({ url: Api.getRecyclePage, params });
};

/**
 * 租户彻底删除
 * @param params
 */
export const deleteLogicTenant = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.deleteLogicTenant, params },{ joinParamsToUrl: true }).then(() => {
    handleSuccess();
  }).catch(()=>{
    handleSuccess();
  });
};

/**
 * 租户还原
 * @param params
 */
export const revertLogicTenant = (params,handleSuccess) => {
  return defHttp.put({ url: Api.revertLogicTenant, params },{ joinParamsToUrl: true }).then(() => {
    handleSuccess();
  })
};


/**
 * 获取当前登录租户名称
 */
export async function getLoginTenantName() {
  let tenantId = getTenantId();
  if(tenantId){
    let result = await getTenantById({ id:tenantId });
    if(result){
      return result.name;
    }
  }
  return "空";
}

/**
 * 保存或者更新用户
 * @param params
 */
export const saveOrUpdateTenantUser = (params, isUpdate) => {
  let url = isUpdate ? Api.editUser : Api.saveUser;
  return defHttp.post({ url: url, params },{ joinParamsToUrl: true });
};
